<?php

function smarty_function_html_cabinet_calendar($params, &$smarty)
{
	$name = 'date';
	$id = '';
	$value = '';
	$class = '';
	$trigger = 'ico-cal-li.gif';
	
	foreach ($params as $_key => $_value)
	{
        switch ($_key)
        {
            case 'name': $name = (string)$_value; break;
            case 'value': if (is_numeric($_value)) { $value = $_value; } break;
            case 'class': $class = (string)$_value; break;
            case 'trigger': $trigger = (string)$_value; break;
            case 'id': $id = (string)$_value; break;
        }
	}
	
	$safe_name = str_replace(array('-','>','<','+'), '_', $name);
	
	$html_result = '<div class="'.$class.'" style="width:92px; height:22px; float:left;">';
	$html_result .= '<input type="text" id="'.$name.'_visible'.$id.'" value="'.($value != '' ? date('d.m.Y', $value) : '').'" onChange="changeDate'.$safe_name.$id.'(); return true" style=" width:55px"" /> ';
	$html_result .= '<img style="cursor:pointer;" align="absmiddle" src="/application/views/scripts/client/images/'.$trigger.'" id="'.$name.'_trigger'.$id.'" style="vertical-align:bottom" onMouseMove="getMouseXY(event); createCalendar'.$safe_name.$id.'();" />';
//	$html_result .= '<a href="javascript:void(0);" id="'.$name.'_trigger'.$id.'" onMouseMove="getMouseXY(event); createCalendar'.$safe_name.$id.'();"><span>Calendar</span></a>';
	$html_result .= '<script type="text/javascript">//<![CDATA[
	    function createCalendar'.$safe_name.$id.'()
		{
			Zapatec.Calendar.setup({
		        firstDay          : 1,
		        weekNumbers       : true,
		        showOthers        : false,
		        showsTime         : false,
		        timeFormat        : "24",
		        step              : 2,
		        range             : [1900.01, 2999.12],
		        electric          : false,
		        singleClick       : true,
		        inputField        : "'.$name.'_visible'.$id.'",
		        button            : "'.$name.'_trigger'.$id.'",
		        ifFormat          : "%d.%m.%Y",
		        daFormat          : "%d.%m.%Y",
		        position		  : [tempX,tempY],
		        align             : "Br"
		      });
		    //]]>
		}
    	function changeDate'.$safe_name.$id.'()
    	{
	    	var date_value = document.getElementById(\''.$name.'_visible'.$id.'\').value;
    		var day = parseInt(date_value.substring(0, 2));
    		var month = parseInt(date_value.substring(3, 5));
    		var yeear = parseInt(date_value.substring(6));
			var humDate = new Date(Date.UTC(yeear, month-1, day, 0, 0, 0));
			var time_stamp = humDate.getTime()/1000.0;
			document.getElementById(\''.$name.$id.'\').value = time_stamp;
		}
	    </script>';
	$html_result .= '<input type="hidden" id="'.$name.$id.'" name="'.$name.'" value="'.$value.'">';
	$html_result .= '</div>';
	
	return $html_result;
}

?>